<template>
  <div class="system-add-role-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" center>
      <el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="井名称">
              <el-input
                  v-model="state.ruleForm.name"
                  placeholder="请输井名称"
                  clearable
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="备注">
              <el-input v-model="state.ruleForm.remake" placeholder="请输入备注" clearable
                        type="textarea" :rows="3" :max-rows="5"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>

      <!-- 弹窗底部按钮 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit(state.dialog.type == 'add' ? 'add' : 'edit')"
                     size="default">{{ state.dialog.submitTxt }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {reactive, ref, computed} from 'vue';
import {addJing, updateJing} from '/@/api/jd/jing/index'
import {formatDateNew} from '/@/utils/formatTime';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义表单引用和状态
const state = reactive({
  ruleForm: {
    id: '',
    name: '',          // 井ID（数字类型
    remake: '',          // 用户ID（数字类型）
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
});

// 打开弹窗
const openDialog = async (type: string, row?: any) => {
  console.log(typeof (type))
  state.dialog.isShowDialog = true;
  state.dialog.type = type;
  if (type === 'edit') {
    // 处理编辑场景：转换时间字符串为Date对象
    state.ruleForm = {
      id: row.id,
      name: row.name,
      remake: row.remake,
    };
    state.dialog.title = '修改井';
    state.dialog.submitTxt = '修 改';
  } else {
    // 新增场景：重置表单
    state.ruleForm = {
      name: '',
      remake: '',
    };
    state.dialog.title = '新增井';
    state.dialog.submitTxt = '新 增';
  }
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};

// 取消操作
const onCancel = () => {
  closeDialog();
};

// 提交操作
const onSubmit = (type: any) => {
  // 示例：打印最终表单数据（实际项目中替换为接口调用）
  console.log('提交的数据：', state.ruleForm);
  if (type === 'add') {
    state.ruleForm = {
      id: state.ruleForm.id,
      name: state.ruleForm.name,
      remake: state.ruleForm.remake,
    }
    addJing(state.ruleForm)
  } else {
    updateJing(state.ruleForm)
  }
  closeDialog();
  emit('refresh');
};

// 暴露组件方法
defineExpose({
  openDialog,
});
</script>

<style scoped>
/* 可选：输入框禁用时的样式调整 */
.el-input[disabled] {
  background-color: #f5f7fa;
  cursor: not-allowed;
}

</style>
